/**
  ******************************************************************************
  * @file    Devices_Receiver.c
  * @author  AnshininTakaha
  * @version V1.21
  * @date		 2021/09/29
  * @brief   The File of the Data Receive of the E22 Module.
	* @copyright (c) 2017-2021 JZI. All rights reserved.
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "Devices_Receiver.h"

#include "Devices_Lidar.h"
#include "Devices_LED.h"

#include "Algorithm_CRC.h"

#include <stdlib.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
uint16_t Receiver_ReceiveDataSumCheck(Lidar_MessageReceiver_t* LMR_In);

/* Private functions ---------------------------------------------------------*/
/**
  * @brief   Receiver Data Check Sum.
  * @param   void
  * @retval  uint16_t return Sumcheck Value.
  */
uint16_t Receiver_ReceiveDataSumCheck(Lidar_MessageReceiver_t* LMR_In)
{
	return (LMR_In->LidarMode + 
					LMR_In->LidarDataState +
					LMR_In->Min_LineDistance + 
					LMR_In->Min_LineAngle +
					LMR_In->Tree_Barrier1_X +
					LMR_In->Tree_Barrier1_Y +
					LMR_In->Tree_Barrier2_X +
					LMR_In->Tree_Barrier2_Y +
					LMR_In->Battary_Voltage) & 0xFFFF;
}

/* Exported functions prototypes ----------------------------------------------------------*/
void Receiver_Reconstruction(Lidar_MessageReceiver_t* LMR_In,char FakeATCommand[]);
void Receiver_Reconstruction_OldProtocol(Lidar_MessageReceiver_t* LMR_In,uint8_t* Fake_Old_Command);
void Receiver_Add_RecBaterry(float recpercent,Lidar_MessageReceiver_t* LMR_In);


/* Exported variables --------------------------------------------------------*/
/* Receiver Functions */
Receiver_Fuc Receiver_Function = Receiver_FucGroundInit;
#undef Receiver_FucGroundInit

/* Receiver LidarPusher Receive Store */
Lidar_MessageReceiver_t Receiver_LidarPusherReceive;

/* Fake AT Command char array store */
char Fake_ATCommand[51];
uint8_t Fake_OldCommand[23];

/* Exported functions --------------------------------------------------------*/
/**
  * @brief   Receiver Data Check and Reconstruction.
  * @param   Lidar_MessageReceiver_t* LMR_In: Input LidarPusher 
	* 				 Package.
  * @retval  void
  */
void Receiver_Reconstruction(Lidar_MessageReceiver_t* LMR_In,char FakeATCommand[])
{
	if(LMR_In->SOF == 0xCDF1 && LMR_In->DataLength == 0x00009)
	{
		uint16_t SumCheck = Receiver_ReceiveDataSumCheck(LMR_In);
		
		if(SumCheck == LMR_In->SumCheck)
		{
			/* LED Tips Show that Receive Board have receive succeed */
			REC_DATA_LED_SetToggle();
			
			/* Use fake ATCommand Sending (Number to formal) */
			char FakeATCommandSet[53] = \
				"+DATA:0,0000,00000,+00000,+00000,+00000,+00000,0000\r\n";
			
			/* 1 LidarMode Bit6*/
			FakeATCommandSet[6] = LMR_In->LidarMode + 48;
			
			/* 2 LidarDistance */
			FakeATCommandSet[8]  = (LMR_In->Min_LineDistance / 1000) + 48;
			FakeATCommandSet[9]  = ((LMR_In->Min_LineDistance % 1000) / 100) + 48;
			FakeATCommandSet[10] = ((LMR_In->Min_LineDistance % 100) / 10) + 48;
			FakeATCommandSet[11] = (LMR_In->Min_LineDistance % 10) + 48;
			
			/* 3 LineAngle */
			FakeATCommandSet[13]  = (LMR_In->Min_LineAngle / 10000) + 48;
			FakeATCommandSet[14] = ((LMR_In->Min_LineAngle % 10000) / 1000) + 48;
			FakeATCommandSet[15] = ((LMR_In->Min_LineAngle % 1000) / 100) + 48;
			FakeATCommandSet[16] = ((LMR_In->Min_LineAngle % 100) / 10) + 48;
			FakeATCommandSet[17] = (LMR_In->Min_LineAngle % 10) + 48;
			
			/* 4 TreeBelong1 X */
			if(LMR_In->Tree_Barrier1_X >= 0){
				FakeATCommandSet[19] = '+';
				
			}
			else{
				FakeATCommandSet[19] = '-';
				LMR_In->Tree_Barrier1_X = -LMR_In->Tree_Barrier1_X;
			}
			FakeATCommandSet[20] = (LMR_In->Tree_Barrier1_X / 10000) + 48;
			FakeATCommandSet[21] = ((LMR_In->Tree_Barrier1_X % 10000) / 1000) + 48;
			FakeATCommandSet[22] = ((LMR_In->Tree_Barrier1_X % 1000) / 100) + 48;
			FakeATCommandSet[23] = ((LMR_In->Tree_Barrier1_X % 100) / 10) + 48;
			FakeATCommandSet[24] = (LMR_In->Tree_Barrier1_X % 10) + 48;
			
			/* 5 TreeBelong1 Y */
			if(LMR_In->Tree_Barrier1_Y >= 0){
				FakeATCommandSet[26] = '+';
			}
			else{
				FakeATCommandSet[26] = '-';
				LMR_In->Tree_Barrier1_Y = -LMR_In->Tree_Barrier1_Y;
			}
			FakeATCommandSet[27] = (LMR_In->Tree_Barrier1_Y / 10000) + 48;
			FakeATCommandSet[28] = ((LMR_In->Tree_Barrier1_Y % 10000) / 1000) + 48;
			FakeATCommandSet[29] = ((LMR_In->Tree_Barrier1_Y % 1000) / 100) + 48;
			FakeATCommandSet[30] = ((LMR_In->Tree_Barrier1_Y % 100) / 10) + 48;
			FakeATCommandSet[31] = (LMR_In->Tree_Barrier1_Y % 10) + 48;
			
			/* 6 TreeBelong2 X */
			if(LMR_In->Tree_Barrier2_X >= 0){
				FakeATCommandSet[33] = '+';
			}
			else{
				FakeATCommandSet[33] = '-';
				LMR_In->Tree_Barrier2_X = -LMR_In->Tree_Barrier2_X;
			}
			FakeATCommandSet[34] = (LMR_In->Tree_Barrier2_X / 10000) + 48;
			FakeATCommandSet[35] = ((LMR_In->Tree_Barrier2_X % 10000) / 1000) + 48;
			FakeATCommandSet[36] = ((LMR_In->Tree_Barrier2_X % 1000) / 100) + 48;
			FakeATCommandSet[37] = ((LMR_In->Tree_Barrier2_X % 100) / 10) + 48;
			FakeATCommandSet[38] = (LMR_In->Tree_Barrier2_X % 10) + 48;
			
			/* 7 TreeBelong2 Y */
			if(LMR_In->Tree_Barrier2_Y >= 0){
				FakeATCommandSet[40] = '+';
			}
			else{
				FakeATCommandSet[40] = '-';
				LMR_In->Tree_Barrier2_Y = -LMR_In->Tree_Barrier2_Y;
			}
			FakeATCommandSet[41] = (LMR_In->Tree_Barrier2_Y / 10000) + 48;
			FakeATCommandSet[42] = ((LMR_In->Tree_Barrier2_Y % 10000) / 1000) + 48;
			FakeATCommandSet[43] = ((LMR_In->Tree_Barrier2_Y % 1000) / 100) + 48;
			FakeATCommandSet[44] = ((LMR_In->Tree_Barrier2_Y % 100) / 10) + 48;
			FakeATCommandSet[45] = (LMR_In->Tree_Barrier2_Y % 10) + 48;
			
			/* 8 Battary_Voltage */
			FakeATCommandSet[47] = (LMR_In->Battary_Voltage / 1000) + 48;
			FakeATCommandSet[48] = ((LMR_In->Battary_Voltage % 1000) / 100) + 48;
			FakeATCommandSet[49] = ((LMR_In->Battary_Voltage % 100) / 10) + 48;
			FakeATCommandSet[50] = (LMR_In->Battary_Voltage % 10) + 48;
			
			/* Memcpy FakeATCommandSet to FakeATCommand */
			memcpy(FakeATCommand,FakeATCommandSet,51);
		}
	}
	else;
}



/**
  * @brief   Receiver Data Check and Reconstruction Old version.
  * @param   Lidar_MessageReceiver_t* LMR_In: Input LidarPusher 
	* 				 Package.
  * @retval  void
  */
void Receiver_Reconstruction_OldProtocol(Lidar_MessageReceiver_t* LMR_In,uint8_t* Fake_Old_Command)
{
	if(LMR_In->SOF == 0xCDF1 && LMR_In->DataLength == 0x00009)
	{
		uint16_t SumCheck = Receiver_ReceiveDataSumCheck(LMR_In);
		
		if(SumCheck == LMR_In->SumCheck)
		{
			/* LED Tips Show that Receive Board have receive succeed */
			REC_DATA_LED_SetToggle();
			
			/* Create Store Buffer */
			uint8_t OldProtocolBuffer[23] = {0};
			
			/* Buffer Store info */
			OldProtocolBuffer[0] = 0x11;
			OldProtocolBuffer[1] = 0x08|0x50;
			OldProtocolBuffer[2] = 0x02;
			
			/* Min_LineDistance */
			OldProtocolBuffer[3] = (uint8_t)((LMR_In->Min_LineDistance>>8)&0xFF);
			OldProtocolBuffer[4] = (uint8_t)(LMR_In->Min_LineDistance&0xFF);
			
			/* Min_LineAngle */
			OldProtocolBuffer[5] = (uint8_t)((LMR_In->Min_LineAngle>>8)&0xFF);
			OldProtocolBuffer[6] = (uint8_t)(LMR_In->Min_LineAngle&0xFF);

			/* TreeBarrier relay 1 X */
			OldProtocolBuffer[7] = (uint8_t)((LMR_In->Tree_Barrier1_X>>8)&0xFF);
			OldProtocolBuffer[8] = (uint8_t)(LMR_In->Tree_Barrier1_X&0xFF);
			
			/* TreeBarrier relay 1 Y */
			OldProtocolBuffer[9] = (uint8_t)((LMR_In->Tree_Barrier1_Y>>8)&0xFF);
			OldProtocolBuffer[10] = (uint8_t)(LMR_In->Tree_Barrier1_Y&0xFF);
			
			/* TreeBarrier relay 2 X */
			OldProtocolBuffer[11] = (uint8_t)((LMR_In->Tree_Barrier2_X>>8)&0xFF);
			OldProtocolBuffer[12] = (uint8_t)(LMR_In->Tree_Barrier2_X&0xFF);
			
			/* TreeBarrier relay 2 Y */
			OldProtocolBuffer[13] = (uint8_t)((LMR_In->Tree_Barrier2_Y>>8)&0xFF);
			OldProtocolBuffer[14] = (uint8_t)(LMR_In->Tree_Barrier2_Y&0xFF);
			
			/* Battary Voltage x 100 */
			OldProtocolBuffer[15] = (uint8_t)((LMR_In->Battary_Voltage>>8)&0xFF);
			OldProtocolBuffer[16] = (uint8_t)(LMR_In->Battary_Voltage&0xFF);
			
			/* Firware_code */
			OldProtocolBuffer[17] = (uint8_t)((VERSION>>8)&0xFF);
			OldProtocolBuffer[18] = (uint8_t)(VERSION&0xFF);
			
			/* REC Voltage */
			OldProtocolBuffer[19] = (uint8_t)((LMR_In->Rec_Voltage>>8)&0xFF);
			OldProtocolBuffer[20] = (uint8_t)(LMR_In->Rec_Voltage&0xFF);
			
			#ifdef RECEIVE_USE_CODE
			OldProtocolBuffer[21] = LMR_In->rssi;
			#endif
			/* CRC Checker */
			OldProtocolBuffer[22] = CRC_Functions.Checksum_CRC8(OldProtocolBuffer,22);
			
			/* Memcpy OldProtocolBuffer to Fake_OldCommand */
			memcpy(Fake_Old_Command,OldProtocolBuffer,23);
		}
	}
}

void Receiver_Add_RecBaterry(float recpercent,Lidar_MessageReceiver_t* LMR_In)
{
	LMR_In->Rec_Voltage = recpercent;
}

/* End -----------------------------------------------------------------------*/
